# -*- coding: utf-8 -*-
# standard
import os
import sys
import json
from datetime import datetime, timedelta, timezone

# third
import requests

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from models.iot_model import ALERT_AUTHORIZATION
from models.iot_model import DATA_AUTHORIZATION
from models.iot_model import MessagePushRecordOrm


"""
@Title:   
@File: temp.py
@Author: walle 2024年12日03日 11时19分34秒
@Version: 1.0.0
@Desc: 
"""


def test():
    url = 'http://iot.ihcc.com.cn:8085/iot/sensor/temperature/77/101'
    # url = 'http://127.0.0.1:8085/iot/sensor/temperature/77/101'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer 4f6160748cca786e56898ad945589d92'
    }
    resp = requests.post(url=url, data='{"temperature": 25.5}', headers=headers)
    print(resp.text)


def send_offline_alert():
    """
    模拟发送离线告警
    """
    url = "http://127.0.0.1:8085/iot/offline_alert/temperature"
    # url = 'http://iot.ihcc.com.cn:8085/iot/offline_alert/temperature'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': ALERT_AUTHORIZATION,
    }
    file_path = os.path.join(_P_PATH, 'examples', '离线警告.json')
    with open(file_path, mode='r', encoding='utf-8') as f:
        data = json.load(f)
        data['_time'] = (datetime.now() - timedelta(hours=8)).strftime("%Y-%m-%dT%H:%M:%SZ")
    resp = requests.post(url=url, json=data, headers=headers)
    resp = resp.text
    print(resp)


def send_temperature_value():
    """
    模拟发送一个温度传感器的信号
    用来模拟告警恢复
    """
    url = "http://127.0.0.1:8085/iot/sensor/temperature/72/44"  # todo: 注意修改 customer_id & store_id
    headers = {
        'Content-Type': 'application/json',
        'Authorization': DATA_AUTHORIZATION,
    }
    file_path = os.path.join(_P_PATH, 'examples', '温度传感器数据.json')
    with open(file_path, mode='r', encoding='utf-8') as f:
        data = json.load(f)
    resp = requests.post(url=url, json=data, headers=headers)
    resp = resp.text
    print(resp)


def send_threshold_alert():
    """
    模拟发送超过阈值告警
    """
    url = "http://127.0.0.1:8085/iot/threshold_alert/temperature"
    headers = {
        'Content-Type': 'application/json',
        'Authorization': ALERT_AUTHORIZATION,
    }
    file_path = os.path.join(_P_PATH, 'examples', '温度超阈值警告.json')
    with open(file_path, mode='r', encoding='utf-8') as f:
        data = json.load(f)['马记永新版']
        data['_time'] = (datetime.now() - timedelta(hours=8)).strftime("%Y-%m-%dT%H:%M:%SZ")
    resp = requests.post(url=url, json=data, headers=headers)
    resp = resp.text
    print(resp)



if __name__ == '__main__':
    
    # send_offline_alert()
    # send_temperature_value()
    send_threshold_alert()
    # print(MessagePushRecordOrm.resend(1))
    pass